package com.example.youhuo.z_a_onepage

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.Nullable
import com.example.myapplication.R

class Splash_Custom_View(context: Context?, @Nullable attrs: AttributeSet?) :
    LinearLayout(context, attrs),
    View.OnClickListener {
    private lateinit var params: LinearLayout.LayoutParams
    private lateinit var list: ArrayList<View>
    private lateinit var imageView: ImageView
    private  var beforview: View? = null
    private lateinit var clickbarlis: ClickBarlistener

    init {
        //初始化
        start()

    }

    fun start() {
        list = ArrayList()
        orientation = HORIZONTAL
        gravity = Gravity.CENTER
        params = LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
        )
        params.weight = 1F


    }

    fun show() {
        for (i in 0..list.size - 1) {
            addView(list.get(i))
            list.get(i).setTag(R.id.text_postion_ket,i)
            list.get(i).setOnClickListener(this)

        }
        //第一次第一个直接点击
        style(list.get(0))
        beforview = list.get(0)
    }

    fun addImage(imgid: Int): Splash_Custom_View {
        imageView = ImageView(context)
        imageView.layoutParams = params
        imageView.setImageResource(imgid)
        imageView.setPadding(10, 10, 10, 10)
        list.add(imageView)
        return this
    }


    fun listener(clickListener: ClickBarlistener) {
        this.clickbarlis = clickListener
    }


    //接口回调
    interface ClickBarlistener {
        fun OnClickItem(postion: Int)
    }

    //点击方法
    override fun onClick(v: View?) {

            if (beforview != null && v!!.getTag(R.id.text_postion_ket)as Int == beforview!!.getTag(R.id.text_postion_ket)as Int) {
                return
            }
            if (clickbarlis != null) {

                clickbarlis.OnClickItem( v!!.getTag(R.id.text_postion_ket)as Int)

            }
        if (v != null) {
            style(v)

            beforview = v
        }

    }
    fun style(selectview: View){
        imagestyle(selectview,true)
        if (beforview == null){
            return
        }
        imagestyle(beforview!!,false)
    }


    private fun imagestyle(selectview: View, b: Boolean) {
        var imageView1 = selectview as ImageView
        if (b) {
            when(selectview.getTag(R.id.text_postion_ket)as Int){
                0-> imageView1.setImageResource(R.mipmap.tabbar_homepage_highlighted_gray)
                1->imageView1.setImageResource(R.mipmap.tabbar_category_highlighted_gray)
                2->imageView1.setImageResource(R.mipmap.tabbar_cart_highlighted_gray)
                3->imageView1.setImageResource(R.mipmap.tabbar_see_highlighted_gray)
                4->imageView1.setImageResource(R.mipmap.tabbar_mine_highlighted_gray)
            }
        }else{
            when(selectview.getTag(R.id.text_postion_ket)as Int){
                0-> imageView1.setImageResource(R.mipmap.tabbar_homepage_normal)
                1->imageView1.setImageResource(R.mipmap.tabbar_category_normal)
                2->imageView1.setImageResource(R.mipmap.tabbar_cart_normal)
                3->imageView1.setImageResource(R.mipmap.tabbar_see_normal)
                4->imageView1.setImageResource(R.mipmap.tabbar_mine_normal)
            }
        }
    }


}